#!/usr/bin/perl -w
use strict;

use DBI;
our $h=DBI->connect('DBI:mysql:database=Down','root') || die $DBI::errstr;
print "Content-Type: text/html\n\n<body onload='window.parent.location=\"add.cgi\"'><pre>\n" unless -t STDIN;
#my @known=map"`$_`",qw/down id url filename size nowsize ip modify added finished status log head/;
#my $table=shift(@known);
#my $pv=join ',',@known;
#my $pq=join ',',map '?',@known;

our $ns=$h->prepare(qq/select id,url,size,post,head from down where status = 0/);
our $hdr=$h->prepare(qq/update down set size=?,head=? where id=?/);

$h->do("truncate table locked");
my $lockput=$h->prepare('insert into locked values (sha1(?),?,?)');
my $lockout=$h->prepare(qq/update down set status=? where id=? and status=0/);

my ($id,$url,$sz,$post,$a);

$ns->execute();
while (($id,$url,$sz,$post,$a)=$ns->fetchrow_array())
{
$lockout->execute(4,$id),next unless $url && $url=~m{^(ht|f)tps?://};
$lockput->execute($url,$id,$$) or $lockout->execute(3,$id) and print("Dupe: $url\n") and next;
next if $sz || $a;
$url=~s{([^\w.\-_=\?\&:/@!%])}{'%'.unpack('H2',$1)}eg;
$a='';
$post=~s{([^\w.\-_=\?\&:/@!%])}{'%'.unpack('H2',$1)}eg,
$a="--post-data='$post' ",
if defined($post) && $post ne '';
#$url=~ s{ftp://}{$&anonymous:gimly%40here\@} if $url =~ m{^ftp://[\w\-\.\:]+/};
$url=~s{ftp\.efind}{ftpr.efind};

print "$url\n";
$_=`HEAD '$url' -C anonymous:gimly\@here`;# -p http://mine:9202/`;
$sz=0;
$sz=$1 if /Content-Length: (\d+)/i;
$hdr->execute($sz,$_,$id);
}

$ns->finish();
$hdr->finish();

$h->disconnect();
print "URL check DONE\n</pre>" unless -t STDIN;

__END__

#our $s=$h->prepare(qq/INSERT ignore $table ($pv) values ($pq)/);

no utf8;
open IN,"don";
open STDERR,'>/dev/null';
while (my $url=<IN>)
{
chomp $url;
#$a=$_;
#print $_,"\n" unless $a eq $_;
$_=`HEAD '$url' -p http://lo:3128/`;
#http://mine:9202/;
my $sz=0;
$sz=$1 if /Content-Length: (\d+)/i;
if ($sz){printf "%5.1f %s\n",$sz/1024/1024,$url}else{print}
}
